using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Web;

namespace Mvc3.Models
{ 
    public class SimpleModelRepository : ISimpleModelRepository
    {
        Mvc3Context context = new Mvc3Context();

        public IQueryable<SimpleModel> All
        {
            get { return context.SimpleModels; }
        }

        public IQueryable<SimpleModel> AllIncluding(params Expression<Func<SimpleModel, object>>[] includeProperties)
        {
            IQueryable<SimpleModel> query = context.SimpleModels;
            foreach (var includeProperty in includeProperties) {
                query = query.Include(includeProperty);
            }
            return query;
        }

        public SimpleModel Find(int id)
        {
            return context.SimpleModels.Find(id);
        }

        public void InsertOrUpdate(SimpleModel simplemodel)
        {
            if (simplemodel.Id == default(int)) {
                // New entity
                context.SimpleModels.Add(simplemodel);
            } else {
                // Existing entity
                context.Entry(simplemodel).State = EntityState.Modified;
            }
        }

        public void Delete(int id)
        {
            var simplemodel = context.SimpleModels.Find(id);
            context.SimpleModels.Remove(simplemodel);
        }

        public void Save()
        {
            context.SaveChanges();
        }
    }

    public interface ISimpleModelRepository
    {
        IQueryable<SimpleModel> All { get; }
        IQueryable<SimpleModel> AllIncluding(params Expression<Func<SimpleModel, object>>[] includeProperties);
        SimpleModel Find(int id);
        void InsertOrUpdate(SimpleModel simplemodel);
        void Delete(int id);
        void Save();
    }
}